#!/bin/bash
#功能函数
funcs=./functions.sh
if test ! -f $funcs; then
    funcs=`which functions.sh`
    if [ "$funcs" = "" ]; then
        echo "functions.sh not exist!!"
        return
    else
        echo "funcs=$funcs"
	. $funcs
    fi
else
    echo "funcs=$funcs"
    . $funcs  
fi

repos_root=/opt/git/repositories

function install_gitosis()
{
saved_dir=$PWD
mkdir -pv ~/src
cd ~/src
#git clone git://eagain.net/gitosis 下载不下来
git clone  git://github.com/res0nat0r/gitosis.git
cd gitosis
sudo python setup.py install
cd $saved_dir
}

function setup_gitweb()
{
  #ubuntu 上安装gitweb(用虚拟机的方式)
  sh_dpkg_install "apache2 gitweb"
  mkdir $HOME/gitweb
  ln -svf /usr/share/gitweb/* $HOME/gitweb
  ln -s $repos_root $HOME/gitweb
  sudo vi /etc/gitweb.conf
  $projectroot = "$HOME/gitweb";

sudo vi /etc/apache2/conf.d/gitweb
<VirtualHost *:80>
ServerName 127.0.0.1
ScriptAlias /gitweb/ /usr/lib/cgi-bin/
DirectoryIndex /gitweb/gitweb.cgi
DocumentRoot /home/clean/gitweb
ErrorLog /var/log/apache2/git.pcware.cn-error.log
CustomLog "/var/log/apache2/git.pcware.cn-access.log" combined
</VirtualHost>
}

#安装 git 服务器
#http://blog.enjoyrails.com/2009/01/06/git服务器安装
#ubuntu下gitosis的安装
#http://www.yooshang.com/archives/276
function install_pkgs()
{
  #安装Git-Core 因为要用到 Gitosis ，所有，需要安装python setuptools
  sh_dpkg_install "git git-core openssh-server python-setuptools gitosis"
  setup_gitweb
  #http://code.douglasthrift.net/files/gitosis/gitosis-0.2.20090917.tar.bz2
  #install_gitosis 通过 apt-get 安装,此步骤取消
  echoTextBlue "setup..... git"
  sudo adduser --system --shell /bin/bash --gecos 'git SCM user' --group git --home /opt/git  git
  #验证失败，大家可以使用下面的方法来创建git用户：sudo useradd -m git sudo passwd git
  scp /home/mhf/.ssh/id_rsa.pub git@172.20.120.64:/opt/git/
  su -l git
  gitosis-init < id_rsa.pub
  chmod 755  /opt/git/repositories/gitosis-admin.git/hooks/post-update
}

function aaaaaaaaaa()
{
mkdir ~/gits/gphone/
cd ~/gits/gphone/
git init
touch README
git add README
git commit -m 'first commit'
git remote add origin git@github.com:mohuifu/gphone.git
git push origin master

mkdir ~/gits/nimo -pv
cd ~/gits/nimo
git init
touch README
git add README
git commit -m 'first commit'
git remote add origin git@github.com:cxnimo/nimo.git
git push -u origin master


test_git=~/git/test_local
mkdir $test_git -pv
cd $test_git
git init
touch test
git add test  
git commit -am "test init"  
git remote add origin git@172.20.120.64:test.git  
git push origin master  



#cat ~/.ssh/id_rsa.pub | xclip -sel clip    #xclip -in README  鼠标中键复制
sudo apt-get install xclip
cat ~/.ssh/id_rsa.pub | xclip -sel clip
ssh-add ~/.ssh/id_rsa

ssh -v git@github.com
ssh-keygen -t dsa
http://help.github.com/troubleshooting-ssh/

使用 Git 管理源代码
工具集在 Linux 上提供了可靠的版本控制功能
http://www.ibm.com/developerworks/cn/linux/l-git/

git@github.com:mohuifu/gphone.git             #ssh 方式访问
https://mohuifu@github.com/mohuifu/gphone.git #http方式访问


github 仓库操作指南：
全局设置：
 下载并安装 Git
  git config --global user.name "Your Name"
  git config --global user.email cxnimo@gmail.com
  Add your public key
下一步：
  mkdir nimo
  cd nimo
  git init
  touch README
  git add README
  git commit -m 'first commit'
  git remote add origin git@github.com:cxnimo/nimo.git
  git push -u origin master
现有的 Git 仓库？

  cd existing_git_repo
  git remote add origin git@github.com:cxnimo/nimo.git
  git push -u origin master
导入现有的 Subversion 仓库？
}
#sudo adduser --system --shell /bin/sh  --gecos 'git version control' --group --disabled-password --home /home/git git


function change_config()
{

    result=`question "Are you setting .git/config？" 1 yes`
    if [ "$result" = "yes" ]; then
        ac_name=`question  "set your name:"  2  mohuifu`
        ac_email=`question  "set your email:"  2  $ac_name@gmail.com`
        if [ "$ac_name" = "" ] || [ "$ac_email" = "" ]; then
             echoText "Warnning: name or email can't be empty!!" RED
             return
        fi
    fi
    ac_config=$1
    if test ! -f $1 ; then
        return
    else
        ac_info=`cat $1 |grep "\[user\]"`	
        if [ "$ac_info" = "" ]; then
echo "
[user]
	name = $ac_name
	email = $ac_name@gmail.com
" >> $ac_config
        else
            sed -i "/name =/c\\	name = $ac_name"  $ac_config
            sed -i "/email =/c\\	email = $ac_name@gmail.com"  $ac_config
        fi
fi
}

#创建一个 git 仓库
function create_project()
{
if [ "$1" = "" ]; then
    result=`question "Are you git init current directory？" 1 yes`
    if [ "$result" = "yes" ]; then
        repos=$PWD
    else
        exit
    fi
else
    repos=$1
fi

if test ! -d $repos; then
    mkdir -pv $repos
fi
cd  $repos
repos_dir=$repos
git init

if test -d  $repos_dir/.git/ ;then
    echoText "exist $repos_dir/.git/!" GREEN
fi
#修改配置文件
change_config $repos_dir/.git/config

result=`question "Are you ignore some files? " 1 yes`
if [ "$result" = "yes" ]; then
    touch  $repos_dir/gitignore
fi
}


function config_http()
{

echo "
Alias /git "/big2/git"
<Location /git/test.git>
    DAV on
    AuthType Basic
    AuthName "Test git freely"
    AuthUserFile "/big2/svn/svnpasswd"
    #Require user Fwolf
    Require valid-user
</Location>
sudo chown www-data:www-data * -R
git clone https://domain.tld/git/test.git gittest
首先遇到的是证书错误，git使用curl，所以证书的处理和svn也不一样，用firefox浏览git网站，在Page Info->View Certificate->Details中把网站的根证书(Issued By/root)导出为一个pem文件，然后将这个pem文件的内容添加到/etc/ssl/certs/ca-certificates.crt的末尾就可以了。接着遇到的是401身份验证错误：
"
}

function config_git_server()
{
echo 
}

function publish_git() 
{
    cd $1
    ac_name=`question  "set your publish .git:"  2  mainfest.git`
    mkdir -pv $PWD/mainfest.git
    GIT_DIR=$PWD/mainfest.git git-init-db
    chmod +x $PWD/mainfest.git/hooks/post-update
    git push  $PWD/mainfest.git/  master
    cd -
  
    mkdir test;cd test  
    repo init -u  git://192.168.2.56/mohuifu.google/mainfest.git
    cd -
    fatal: manifest 'default.xml' not available
    fatal: manifest default.xml not found


http://hilbert.spaces.live.com/?_c11_BlogPart_BlogPart=blogview&_c=BlogPart&partqs=cat%3Dgit
1、Create  github Account
eg.  you used the email:xxxxx@gmail.com
2、Download && install MsysGit
  Then open the git bash , input the commends
  $ git config --global user.name "Your Name"
  $ git config --global user.email xxxxx@gmail.com

3、Create SSH-KEY
  $ ssh-keygen  -C  ' xxxxx@gmail.com'  -t  rsa
  # The git bash ask you to enter something ,  but you can Skip everything ,
  just push the " ENTER " key, when it s finished,you need open the file: id_rsa.pub
  And copy the key. 
}

#SVN+GIT=鱼与熊掌兼得
#http://www.svn8.com/svnpz/20080619/821.html
function git_get()
{
    git clone   mhf@192.168.2.56:~/git/mohuifu.google/mainfest.git  ./test
    git clone   mhf@192.168.2.56:~/git/mohuifu.google/  ./test

    #git 安装 git 服务器启动脚本
    #启动 git 服务器
    git_dir=$HOME/git/
    git-daemon --verbose --export-all --base-path=$git_dir

    git clone   git://192.168.2.56/mohuifu.google/  test1
    git clone   git://192.168.2.56/mohuifu.google/mainfest.git  test2
}

#帮助
function usage()
{
echoText "
$1 setup                      : setup git service
$1 create [ project_name ]    : create a git repos
$1 --help                     : get some help
" RED
}

if [ "$1" = "" ]; then
    result=`question "Do you setup git server？" 1 yes`
    if [ "$result" = "yes" ]; then
        install_pkgs
    fi
    exit
else
    if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
        usage $0
    elif [ "$1" = "create" ]; then
	create_project $2
        publish_git $2
    elif [ "$1" = "setup" ]; then
        install_pkgs
    else
        usage $0
    fi
fi
exit
参考资料：
Git入门教程
http://hi.baidu.com/eehuang/blog/item/37af8d54242d6351564e00b5.html

http://www.bitsun.com/documents/gittutorcn.htm
SVN+GIT=鱼与熊掌兼得
http://www.svn8.com/svnpz/20080619/821.html



